﻿#include "form_alert.h"
#include "ui_form_alert.h"

#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif



struct FAULT
{
public:
	QString device;
	QString dateTime;
	QString cause;
	QString level;
	QString deal;

public:
	FAULT() {
	}
	FAULT(QString v1, QString v2, QString v3, QString v4, QString v5)
	{
		device = v1;
		dateTime = v2;
		cause = v3;
		level = v4;
		deal = v5;
	}

	QJsonObject toJson()
	{
		QJsonObject jObj;
		jObj.insert("device", device);
		jObj.insert("dateTime", dateTime);
		jObj.insert("cause", cause);
		jObj.insert("level", level);
		jObj.insert("deal", deal);

		return jObj;
	}

	void fromJson(QJsonObject obj)
	{
		device = obj.value("device").toString();
		dateTime = obj.value("dateTime").toString();
		cause = obj.value("cause").toString();
		level = obj.value("level").toString();
		deal = obj.value("deal").toString();
	}
};

QList< FAULT* > fault_info
{
	new FAULT("手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "异常", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "报警", "未处理"),
	new FAULT("手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "异常", "未处理"),
	new FAULT("手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "报警", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "报警", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "报警", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "报警", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "报警", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "报警", "未处理"),
	new FAULT("手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "报警", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "报警", "未处理"),
	new FAULT("核反应堆", "2023/5/30 10:54:35", "温度过高，请注意降温", "异常", "未处理"),
	new FAULT("手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "报警", "未处理"),
	new FAULT("手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "报警", "未处理")

};

void FormAlert::saveJson()
{
	QDir dir(qApp->applicationDirPath());
	if (!dir.exists("data"))
		dir.mkpath("data");

	QJsonArray headerArray;
	QStringList header_label;
	header_label << "序号" << "操作ID" << "故障设备" << "故障时间" << "故障原因" << "故障等级" << "是否处理" << "操作时间" << "操作";
	//headerArray.fromStringList(header_label);
	foreach(QString str, header_label)
	{
		headerArray.append(str);

	}

	QJsonArray dataArray;
	foreach(FAULT * json, fault_info) {
		dataArray.append(json->toJson());
	}


	QJsonObject content;
	content["header"] = headerArray;
	content["data"] = dataArray;

	QFile fileArray(QApplication::applicationDirPath() + "/data/jsonFault.json");
	if (fileArray.open(QIODevice::WriteOnly))
	{
		QByteArray bytes = QJsonDocument(content).toJson();
		fileArray.write(bytes);
		fileArray.close();
	}
}

bool FormAlert::readJson()
{

	QFile fileArray(QApplication::applicationDirPath() + "/data/jsonFault.json");
	if (fileArray.open(QIODevice::ReadOnly))
	{
		fault_info.clear();

		QByteArray bytes = fileArray.readAll();
		QJsonDocument doc = QJsonDocument::fromJson(bytes);

		QJsonObject Array = doc.object();
		QJsonArray headerArray = Array.value("header").toArray();
		QJsonArray dataArray = Array.value("data").toArray();


		int cnt = dataArray.count();
		for (int i = 0; i < cnt; i++)
		{
			QJsonObject  obj = dataArray.at(i).toObject();
			FAULT* fault = new FAULT;
			fault->fromJson(obj);
			fault_info.append(fault);
		}

		return true;
	}
	return false;
}

FormAlert::FormAlert(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::FormAlert)
{
	ui->setupUi(this);


	if (!readJson())
		saveJson();

	init_tableview();

	ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	//ui->tableView->resizeColumnsToContents();

	ui->tableView->setColumnWidth(0, 80);
	ui->tableView->setColumnWidth(1, 180);
	ui->tableView->setColumnWidth(2, 180);
	ui->tableView->setColumnWidth(3, 380);
	ui->tableView->setColumnWidth(4, 180);
	ui->tableView->setColumnWidth(5, 180);
}

FormAlert::~FormAlert()
{
	delete ui;
}


void FormAlert::init_tableview()
{
	QStringList header_label;
	header_label << "序号" << "故障设备" << "报警时间" << "故障原因" << "故障等级" << "是否处理" << "操作";

	model_data = new QStandardItemModel;
	model_data->setHorizontalHeaderLabels(header_label);
	model_data->setRowCount(fault_info.count());

	ui->tableView->setModel(model_data);

	update_tableview();

	int row = 0;
	foreach(FAULT * fault, fault_info)
	{

		QModelIndex index_6 = model_data->index(row, 6);

		QPushButton* btn = new QPushButton("处理");
		ui->tableView->setIndexWidget(index_6, btn);

		connect(btn, &QPushButton::clicked, [=] {
			fault->deal = QString("已处理");
			saveJson();

			update_tableview();
			});

		row++;
	}

}

void FormAlert::update_tableview()
{
	//model_data->clear();
	//QStringList header_label;
	//header_label << "序号" << "故障设备" << "报警时间" << "故障原因" << "故障等级" << "是否处理" << "操作";
	//model_data->setHorizontalHeaderLabels(header_label);
	//model_data->setRowCount(156);

	int row = 0;
	foreach(FAULT * fault, fault_info)
	{
		QModelIndex index_0 = model_data->index(row, 0);
		model_data->setData(index_0, row + 1);

		QModelIndex index_1 = model_data->index(row, 1);
		model_data->setData(index_1, fault->device);

		QModelIndex index_2 = model_data->index(row, 2);
		model_data->setData(index_2, fault->dateTime);

		QModelIndex index_3 = model_data->index(row, 3);
		model_data->setData(index_3, fault->cause);

		QModelIndex index_4 = model_data->index(row, 4);
		model_data->setData(index_4, fault->level);
		if (fault->deal == "未处理")
		{
			if (fault->level == "异常")
				model_data->setData(index_4, QBrush(QColor(Qt::blue)), Qt::BackgroundColorRole);
			if (fault->level == "报警")
				model_data->setData(index_4, QBrush(QColor(Qt::red)), Qt::BackgroundColorRole);
		}
		else
			model_data->setData(index_4, QBrush(QColor(Qt::gray)), Qt::BackgroundColorRole);

		QModelIndex index_5 = model_data->index(row, 5);
		model_data->setData(index_5, fault->deal);



		row++;
	}
}
